package com.shkmall.goods.controller;


import com.shkmall.common.pojo.Page;
import com.shkmall.common.pojo.PageResult;
import com.shkmall.common.pojo.Result;
import com.shkmall.common.pojo.StatusCode;
import com.shkmall.goods.pojo.Brand;
import com.shkmall.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

@RequestMapping("/brand")
@RestController
@CrossOrigin  /*开启跨域请求*/
public class BrandController {
    @Autowired
    private BrandService brandService;

    /*查询列表*/
    @GetMapping("/findList")
    public Result findList(){
        List<Brand> data = brandService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",data);
    }

    /*添加*/
    @PostMapping( "/add")
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }
    /*修改*/
    @PutMapping("/update/{id}")
    public Result update(@RequestBody  Brand brand,@PathVariable("id") Integer id){
        brand.setId(id);
       brandService.update(brand);
       return new Result(true,StatusCode.OK,"修改成功");
    }
    /*删除*/
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
            brandService.delete(id);
            return new Result(true,StatusCode.OK,"删除成功");
    }
    /*条件查询*/
    @GetMapping("/search")
    public Result searchCondition(@RequestParam Map searchMap){
       List<Brand> data =  brandService.searchCondition(searchMap);
       return new Result(true,StatusCode.OK,"条件查询成功",data);
    }

    /*分页查询*/
    @GetMapping("/search/{page}/{size}")
    public Result searchPage(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
      PageResult<Brand> data= brandService.findPage(page,size);
      return new Result(true,StatusCode.OK,"分页查询成功",data);
    }

    /*分页+条件查询*/
    @GetMapping("/searchPage/{page}/{size}")
    public Result search(@RequestParam Map searchMap,@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        PageResult<Brand> data = brandService.search(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功",data);
    }

    /*根据分类名称查询品牌列表*/
    @GetMapping("/category/{categoryName}")
    public Result findBrandByCategoryName(@PathVariable("categoryName")String categoryName){
       List<Brand> data = brandService.findBrandByCategoryName(categoryName);
       return new Result(true,StatusCode.OK,"查询成功",data);
    }
}
